<template>
  <div>
    <div>
      <div>
        <swiper indicator-dots="true"
                autoplay="true"
                indicator-color="#f8f8f9"
                indicator-active-color="#19be6b"
                style="height: 421rpx">
          <div v-for="image_item in image_urls" :key="index">
            <swiper-item>
              <img :src="image_item" style="width: 100%; height: 100%">
            </swiper-item>
          </div>
        </swiper>
      </div>
      <div v-for="(item, index) in indexArr" style="background:#ffffff;padding: 20rpx; shadow: true; margin: 10rpx" :key="index" @tap="toEvaluation(item.id)">
        <Card :bordered="false">
          <p slot="title" style="font-size: 14pt;">{{ item.content }}</p>
          <p style="font-size: 11pt; color: #888888;" @tap="toThemeDetail(item.topicId)" @click.stop>主题: {{item.topicName}}</p>
        </Card>
      </div>
    </div>
  </div>
</template>


<script>
  export default {
    data () {
      return {
        URL: this.GLOBAL.apiUrl, // 服务器地址
        indexArr: [], // 存放句子信息
        userId: [], // 存放从服务器获取的openId
        index: 0, // 标识句子列表的数组下标，用于页面间跳转参数传递
        image_urls: [ // 轮播图图片路径
          '../../static/swiper/a.jpg',
          '../../static/swiper/b.jpg',
          '../../static/swiper/c.jpg',
          '../../static/swiper/d.jpg',
          '../../static/swiper/e.jpg',
          '../../static/swiper/f.jpg',
          '../../static/swiper/g.jpg',
          '../../static/swiper/h.jpg'
        ]
      }
    },
    component: {
    },
    beforeMount () {
      // 挂载前调用后台api打印首页并调用wx.login发送jscode到后台获取openId
      this.$fly.get(this.URL + 'topic/getTopic')
        .then((response) => {
          let themeArr = response.data.data
          this.$store.dispatch('getThemeArr', themeArr)
          this.themeArr = themeArr
        })
        .catch((error) => {
          console.log(error)
        })
      let _that = this
      wx.login({
        success (res) {
          if (res.code) {
            wx.request({
              url: _that.URL + 'user/sendUserId',
              method: 'POST',
              data: {
                code: res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success (res) {
                let userId = res.data.data
                _that.$store.dispatch('getUserId', userId)
                _that.userId = userId
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
      this.$fly.get(_that.URL + 'sentence/getSentence')
        .then((response) => {
          let indexArr = response.data.data
          this.$store.dispatch('getIndexArr', indexArr)
          this.indexArr = indexArr
        })
        .catch((error) => {
          console.log(error)
        })
    },
    methods: {
      toEvaluation (sid) {
        // 跳转到评测页
        wx.navigateTo({
          url: '/pages/evaluation/main?sid=' + sid
        })
      },
      toThemeDetail (topicId) {
        // 跳转到主题详情页
        wx.navigateTo({
          url: '/pages/theme_detail/main?topicId=' + topicId
        })
      }
    }
  }
</script>

<style>

</style>
